#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fb.h>

#include <linux/input.h>//触摸屏的头文件

//液晶屏设备     触摸屏设备 


void initTs(int *tsFd){
    *tsFd = open("/dev/input/event0", O_RDWR);
    if(*tsFd == -1)
    {
        perror("open touch error");
        return;
    }
}

int touch_wait(int tsFd,int *x,int *y)
{
    //1 打开触摸屏设备文件  touch sreen
    // int tsFd = open("/dev/input/event0", O_RDWR);
    int countX = 0,countY = 0;
    int flag = 0;
    // if(tsFd == -1)
    // {
    //     perror("open touch error");
    //     return;
    // }
    while(1){
        //2 读取触摸屏设备的坐标
        struct input_event info;
        printf("type:%d, code:%d, value:%d\n", info.type, info.code, info.value);
        read(tsFd,&info,sizeof(info));
        if(info.type == EV_ABS && info.code == ABS_X){
            *x = (info.value * 800 *1.0) / 1024;
            countX++;
        }
        if(info.type == EV_KEY && info.code == BTN_TOUCH&&info.value==0){
            flag = 1;
        }
        if(countX == 3){
            break;
        }
        // printf(" ");
        if(info.type == EV_ABS && info.code == ABS_Y){
            *y = (info.value * 480 * 1.0) / 600;
            // printf("x:%d,y:%d\n",*x,*y);
            break;
        }
        
    }
    return flag;
    
    //4 关闭触摸屏文件
    // close(tsFd);
}


